/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.event;

import java.awt.geom.Point2D;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;

public class WheelZoomEventHandler extends PBasicInputEventHandler implements CheckConsumedInputEventHandler {
    
    public boolean eventProcessed(PInputEvent event, int type) {
        m_processed = false;
        processEvent(event, type);
        return m_processed;
    }
    

    public void mouseWheelRotated(PInputEvent event) {
        // The wheel rotation doesn;t interfere with draggin, panning and seletcion,
        // so is not dependent on active state
        Point2D viewZoomPoint = event.getPosition();
        PCamera camera = event.getCamera();
        double scaleFactor = (1.0 - (double)(event.getWheelRotation())*0.05);
        
        
        camera.scaleViewAboutPoint(scaleFactor, viewZoomPoint.getX(), viewZoomPoint.getY());
        // Don't zoom out too far
        if ( camera.getViewScale() <= m_minScale ) {
            camera.setViewScale(m_minScale);
        }
        m_processed = true;
    }
    
    private double m_minScale = 0.01;
    private boolean m_processed = false;

}
